`
https://leetcode.cn/problems/add-two-numbers/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
  let p1 = l1, p2 = l2
  // 虚拟链表头结点
  const dummy = {
    next: null
  }
  // 新链表指针
  let p = dummy
  // 记录进位
  let carry = 0
  while (p1 || p2 || carry > 0) {
    // 计算本次新结点的值
    let val = carry
    if (p1) {
      val += p1.val
      p1 = p1.next
    }
    if (p2) {
      val += p2.val
      p2 = p2.next
    }
    // 处理进位
    carry = Math.floor(val / 10)
    val %= 10
    // 创建新结点
    p.next = new ListNode(val)
    p = p.next
  }

  return dummy.next
};